package com.eastfair.home.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eastfair.boot.request.PageParams;
import com.eastfair.common.api.ServiceContractFeign;
import com.eastfair.common.dto.AttachmentFileDTO;
import com.eastfair.common.vo.AttachmentFileVO;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.BizException;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.home.enumeration.HomeMoudleEnum;
import com.eastfair.home.service.ContractService;
import com.eastfair.pay.api.AttachmentFileFeign;
import com.eastfair.pay.dto.ServiceContractPageQuery;
import com.eastfair.pay.dto.ServiceContractSaveDTO;
import com.eastfair.pay.dto.ServiceContractUpdateDTO;
import com.eastfair.pay.vo.ServiceContractVO;
import com.eastfair.util.OrderUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 业务实现类
 * 合同
 * </p>
 *
 * @author lyw
 * @date 2022-07-29
 */
@Slf4j
@Service
public class ContractServiceImpl implements ContractService {

    @Autowired
    private ServiceContractFeign serviceContractFeign;
    @Autowired
    private AttachmentFileFeign attachmentFileFeign;

    private static final String CONTRACT_CODE="CN";
    /**
     * 保存合同
     * @param serviceContractSaveDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveContract(ServiceContractSaveDTO serviceContractSaveDTO) {
        log.info("ContractServiceImpl +++++ saveContract - 保存合同 params={}", JSONObject.toJSON(serviceContractSaveDTO));
        String code = OrderUtil.getOrderNo(CONTRACT_CODE);
        serviceContractSaveDTO.setContractNo(code);
        R<ServiceContractSaveDTO> r = serviceContractFeign.save(serviceContractSaveDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        //展厅附件集合
        List<AttachmentFileDTO> attachmentFileDTOList = serviceContractSaveDTO.getAttachmentFileDTOs();
        if (Objects.nonNull(attachmentFileDTOList) && attachmentFileDTOList.size() > 0) {
            attachmentFileDTOList.forEach((s) -> {
                s.setBusinessId(r.getData().getId());
                s.setBusinessType(HomeMoudleEnum.COMPLEMENT_CONTRACT_ANNEX.getCode());
            });
            //添加展厅图纸附件
            attachmentFileFeign.addAttachmentFile(attachmentFileDTOList);
            log.info("ContractServiceImpl +++++ saveContract -》》》》 添加合同附件成功");
        }
        return Boolean.TRUE;
    }

    /**
     * 修改合同
     * @param serviceContractUpdateDTO
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateContract(ServiceContractUpdateDTO serviceContractUpdateDTO) {
        log.info("ContractServiceImpl +++++ saveContract - 保存合同 params={}", JSONObject.toJSON(serviceContractUpdateDTO));
        //修改合同信息
        R<ServiceContractUpdateDTO> r = serviceContractFeign.update(serviceContractUpdateDTO);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        List<AttachmentFileDTO> attachmentFileDTOList = serviceContractUpdateDTO.getAttachmentFileDTOs();
        //删除合同附件
        AttachmentFileDTO attachmentFileDTO = new AttachmentFileDTO();
        attachmentFileDTO.setBusinessId(serviceContractUpdateDTO.getId());
        attachmentFileDTO.setBusinessType(HomeMoudleEnum.COMPLEMENT_CONTRACT_ANNEX.getCode());
        R<List<AttachmentFileVO>> listR = attachmentFileFeign.queryAttachmentFile(attachmentFileDTO);
        if (Objects.nonNull(listR) && Objects.nonNull(listR.getData()) && listR.getData().size() > 0) {
            listR.getData().forEach((s) -> {
                attachmentFileFeign.detele(s.getId());
            });
        }
        //修改合同附件
        if (Objects.nonNull(attachmentFileDTOList) && attachmentFileDTOList.size() > 0) {
            attachmentFileDTOList.forEach((s) -> {
                s.setBusinessId(serviceContractUpdateDTO.getId());
                s.setBusinessType(HomeMoudleEnum.COMPLEMENT_CONTRACT_ANNEX.getCode());

            });
            //修改展厅图纸附件
            attachmentFileFeign.addAttachmentFile(attachmentFileDTOList);
            log.info("SpaceServiceImpl +++++ saveOrUpdateAttachmentFile -》》》》 修改展厅图纸附件成功");
        }

        return Boolean.TRUE;
    }

    /**
     * 删除合同
     * @param ids
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteContract(List<Long> ids) {
        log.info("ContractServiceImpl +++++ deleteContract - 保存合同 params={}", JSONObject.toJSON(ids));
        R<Boolean> r = serviceContractFeign.deleteOfStatus(ids);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        return r.getData();
    }

    /**
     * 合同列表
     * @param params
     * @return
     */
    @Override
    public Page<ServiceContractVO> queryPage(PageParams<ServiceContractPageQuery> params) {
        log.info("ContractServiceImpl +++++ queryPage - 合同列表 params={}", JSONObject.toJSON(params));
        R<Page<ServiceContractVO>> r = serviceContractFeign.queryPage(params);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        Page<ServiceContractVO> resultPage = ConvertUtil.convertPage(r.getData(), ServiceContractVO.class);
        if(Objects.nonNull(resultPage) && Objects.nonNull(resultPage.getRecords()) && resultPage.getRecords().size()>0){
            for(ServiceContractVO s:  resultPage.getRecords()){
                AttachmentFileDTO attachmentFileDTO = new AttachmentFileDTO();
                attachmentFileDTO.setBusinessId(s.getId());
                attachmentFileDTO.setBusinessType(HomeMoudleEnum.COMPLEMENT_CONTRACT_ANNEX.getCode());
                R<List<AttachmentFileVO>> listR = attachmentFileFeign.queryAttachmentFile(attachmentFileDTO);
                if (Objects.nonNull(listR) && Objects.nonNull(listR.getData()) && listR.getData().size() > 0) {
                    s.setAttachmentFileVOList(listR.getData());
                }
            }
        }
        return resultPage;
    }

    /**
     * 通过合同id获取详情
     * @param id
     * @return
     */
    @Override
    public ServiceContractVO getContractById(Long id) {
        R<ServiceContractVO> r = serviceContractFeign.getContractById(id);
        if(!r.getIsSuccess()){
            throw BizException.wrap(r.getCode(),r.getMsg());
        }
        AttachmentFileDTO attachmentFileDTO = new AttachmentFileDTO();
        attachmentFileDTO.setBusinessId(r.getData().getId());
        attachmentFileDTO.setBusinessType(HomeMoudleEnum.COMPLEMENT_CONTRACT_ANNEX.getCode());
        R<List<AttachmentFileVO>> listR = attachmentFileFeign.queryAttachmentFile(attachmentFileDTO);
        if (Objects.nonNull(listR) && Objects.nonNull(listR.getData()) && listR.getData().size() > 0) {
            r.getData().setAttachmentFileVOList(listR.getData());
        }
        return r.getData();
    }

}
